package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;


    @Override
    public PageInfo selectTravelById(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list = travelMapper.selectForLists(qo);
        return new PageInfo(list);
    }

    @Override
    public void save(Travel t) {
        t.setCreateTime(new Date());
        t.setLastUpdateTime(new Date());
        t.setState(Travel.STATE_NORMAL);
        travelMapper.insert(t);
        TravelContent content = t.getContent();
        if (content != null) {
            content.setId(t.getId());
            contentMapper.insert(content);
        }
    }

    @Override
    public PageInfo selectTravels(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.list(qo);
        return new PageInfo(list);
    }

    @Override
    public void updateStateById(Travel t) {
        if (t.getState() == Travel.STATE_RELEASE) {
            t.setReleaseTime(new Date());
        }
        travelMapper.updateState(t);
    }

    @Override
    public TravelContent selectTravelContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public Travel selectOne(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Travel travel) {
        travel.setLastUpdateTime(new Date());
        travelMapper.updateByPrimaryKey(travel);

        contentMapper.updateByPrimaryKey(travel.getContent());

    }

    @Override
    public PageInfo query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.list(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo selectTravelAndUserByAuthorId(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        List<Object> list = new ArrayList<>();
        List<Travel> travels = travelMapper.selectTravelAndUserByAuthorId(qo);
        for (Travel travel : travels) {
            List<User> userBys = travel.getUserBy();
            for (User userBy : userBys) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("travel", travel);
                map.put("userBy", userBy);
                list.add(map);
            }
        }
        return new PageInfo(list);
    }

    @Override
    public List<Travel> selectTravelsByStrategyId(Long strategyId) {
        List<Travel> list= travelMapper.selectTravelsByStrategyId(strategyId);
        return list ;
    }

    @Override
    public void updateViewCount(Long travelId) {
        travelMapper.updateViewCount(travelId);
    }

    @Override
    public Integer selectTravelCount(Long authorId) {

        return travelMapper.selectTravelCount(authorId);
    }

    @Override
    public List<Map<String, Object>> selectTravelByKeyword(String keyword) {

        return travelMapper.selectTravelByKeyword(keyword);
    }

}
